﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BISPOS
{
    public partial class DevideProductXtraForm : DevExpress.XtraEditors.XtraForm
    {
        private Product oldProduct;
        private Product newProduct;
        private BISPOS.Repository.ProductRepository repository = new Repository.ProductRepository();
        private BISPOS.Repository.BillRepository billRepository = new Repository.BillRepository();
        private BISPOS.Repository.StockRepository stockRepository = new Repository.StockRepository();
        public DevideProductXtraForm()
        {
            InitializeComponent();
        }

        private void oldBarcodeText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                oldProduct = repository.Get(oldBarcodeText.Text);
                if (oldProduct != null)
                {
                    if (oldProduct.CapitalCost == 0)
                    {
                        MessageBox.Show("ยังไม่มี Stock สินค้าที่ต้องการแบ่ง ทำให้ราคาทุนของสินค้าใหม่เป็น 0");
                        oldBarcodeText.ResetText();
                        oldBarcodeText.Focus();
                    }
                    else
                    {
                        oldProductText.Text = oldProduct.Name;

                        oldQuantityTextEdit.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("ไม่พบสินค้า");
                    oldBarcodeText.ResetText();
                }

            }
        }

        private void newBarcodeText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                newProduct = repository.Get(newBarcodeText.Text);
                if (newProduct != null)
                {
                    newProductText.Text = newProduct.Name;
                    newQuantityPerUnitTextEdit.Focus();
                }
                else
                {
                    MessageBox.Show("ไม่พบสินค้า");
                    oldBarcodeText.ResetText();
                }
            }
        }

        private void textEdit6_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                newCapitalPriceText.Text =
                    (oldProduct.CapitalCost / Convert.ToDecimal(newQuantityPerUnitTextEdit.Text)).ToString("N2");
                SumQuantityPerUniTextEdit.Text =
                    (Convert.ToDecimal(oldQuantityTextEdit.Text) * Convert.ToDecimal(newQuantityPerUnitTextEdit.Text)).ToString("N2");
            }
            catch { }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
            try
            {
                List<BillItem> billItems = new List<BillItem>();
                Stock stock;
                if (newProduct.IsAvailableStock)
                {
                    if (newProduct.FirstAvailableStock.CurrentQuantity > 0)
                    {
                        billItems.Add(new BillItem
                        {
                            Stock = new Stock
                            {
                                BuyingCost = Convert.ToDecimal(oldCapitalPriceText.Text),
                                CurrentQuantity = Convert.ToDouble(SumQuantityPerUniTextEdit.Text),
                                Date = DateTime.Now,
                                OriginalQuantity = Convert.ToDouble(SumQuantityPerUniTextEdit.Text),
                                ProductId = newProduct.Id
                            },
                        });
                    }
                    else
                    {
                        stock = newProduct.FirstAvailableStock;
                        if ((stock.CurrentQuantity + Convert.ToDouble(SumQuantityPerUniTextEdit.Text)) >= 0)
                        {
                            stock.OriginalQuantity = Convert.ToDouble(SumQuantityPerUniTextEdit.Text);
                            stock.CurrentQuantity += Convert.ToDouble(SumQuantityPerUniTextEdit.Text);
                            stock.BuyingCost = Convert.ToDecimal(oldCapitalPriceText.Text);
                            stock.Date = DateTime.Now;
                            db.SubmitChanges();
                            billItems.Add(new BillItem
                            {
                                Stock = stock
                            });
                        }
                        else
                        {
                            double leftQuantity = stock.CurrentQuantity + Convert.ToDouble(SumQuantityPerUniTextEdit.Text);
                            stock = newProduct.FirstAvailableStock;
                            stock.OriginalQuantity = Convert.ToDouble(SumQuantityPerUniTextEdit.Text);
                            stock.CurrentQuantity = 0;
                            stock.BuyingCost = Convert.ToDecimal(oldCapitalPriceText.Text);
                            db.SubmitChanges();

                            billItems.Add(new BillItem
                            {
                                StockId = stock.Id
                            });
                            stockRepository.AddNewStock(newProduct.Id,
                                0, leftQuantity, 0, DateTime.Now);
                        }
                    }
                }

                
                if (oldProduct.IsAvailableStock)
                {
                    stockRepository.SubtractStock(oldProduct.FirstAvailableStock.Id,
                       Convert.ToDouble(oldQuantityTextEdit.Text));
                }
                else
                {
                    stockRepository.AddNewStock(oldProduct.Id, 0,
                        -Convert.ToDouble(oldQuantityTextEdit.Text),
                        0, DateTime.Now);
                }

                billRepository.AddBill(null, DateTime.Now, String.Format("แบ่งจากสินค้า : {0}  เป็น {1} ชิ้น", oldProduct.BarcodeAndName, newQuantityPerUnitTextEdit.Text),
                    false, billItems);
                this.Close();
            }
            catch { MessageBox.Show("ข้อมูลไม่ถูกต้อง"); }
        }



        private void oldQuantityTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                oldCapitalPriceText.Text = (oldProduct.CapitalCost * Convert.ToDecimal(oldQuantityTextEdit.Text)).ToString("N2");
            }
            catch { }
        }
    }
}